/**
 * 
 */
package com.lmm.car.content;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.lmm.car.LoginEasyPass;
import com.lmm.http.client.HttpClientUtils2;
import com.lmm.test.TestLogin;
import com.lmm.util.ContentUtils;

/**
 * @author leiming
 *
 */
public class EasyPassContent {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		LoginEasyPass.login();
		addContent();
	}

	public static String addContent() throws Exception{
		CloseableHttpClient httpclient = HttpClientUtils2.getHttpClient(); 
		InputStream in = EasyPassContent.class.getResourceAsStream("yi-new-content.txt");
		List<NameValuePair> listP = ContentUtils.gerneretNameValuePair(in);
		//添加初始参数
		Map<String,String> param = getInitParameter();
		for(Map.Entry<String, String> e:param.entrySet()){
			listP.add(new BasicNameValuePair(e.getKey(),e.getValue()));
		}
		@SuppressWarnings("deprecation")
		HttpEntity entityP = new UrlEncodedFormEntity(listP, HTTP.UTF_8);
        HttpUriRequest addContent = RequestBuilder.post()
        		.setUri("http://das.app.easypass.cn/InforManage/News/TemplateNews.aspx")
                .setEntity(entityP)
                .build();
        addContent.setHeader("Accept", "*/*");
        addContent.setHeader("X-Requested-With","XMLHttpRequest");
        addContent.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
        addContent.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        addContent.setHeader("User-Agent", HttpClientUtils2.userAgent);
        String content  = httpclient.execute(addContent,HttpClientUtils2.basicResponseHandler);
        System.out.println("content:"+content);
        return content;
	}

	/**
	 * 获取登录初始化参数
	 * @return
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static Map<String,String> getInitParameter() throws ClientProtocolException, IOException{
		Map<String,String> param = new HashMap<String, String>();
		CloseableHttpClient httpclient = HttpClientUtils2.getHttpClient(); 
		HttpGet httpget = new HttpGet("http://das.app.easypass.cn/InforManage/News/TemplateNews.aspx");
        httpget.setHeader("User-Agent", HttpClientUtils2.userAgent);
        String content  = httpclient.execute(httpget,HttpClientUtils2.basicResponseHandler);
        Document doc =  Jsoup.parse(content);
        String tag = "#__VIEWSTATE";
        Elements els =  doc.select(tag);
        Element e = els.first();
        String __VIEWSTATE = e.attr("value");
        param.put("__VIEWSTATE", __VIEWSTATE);
        return param;
	}
}
